<template>
  <el-container>
    <el-aside width="200px">
      <el-menu default-active="1-2">
        <el-submenu index="1">
          <template slot="title"><i class="el-icon-news"></i>文章</template>
          <el-menu-item-group>
            <el-menu-item index="1-1" @click="toggleTab('Editor','create')">发布文章</el-menu-item>
            <el-menu-item index="1-2" @click="toggleTab('Article', 'update')">管理文章</el-menu-item>
          </el-menu-item-group>
        </el-submenu>
        <el-submenu index="2">
          <template slot="title"><i class="el-icon-setting"></i>用户</template>
          <el-menu-item index="2-1" @click="toggleTab('User')">用户管理</el-menu-item>
        </el-submenu>
        <el-submenu index="3">
          <template slot="title"><i class="el-icon-warning"></i>客户</template>
          <el-menu-item index="3-1" @click="toggleTab('Consumer','create')">投资计算/留言管理</el-menu-item>
        </el-submenu>
      </el-menu>
    </el-aside>
    <el-container>
      <el-header>
        <el-dropdown @command="handleCommand">
          <i class="el-icon-setting" style="margin-right: 15px"></i>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="logout">注销</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
        <img src="../assets/user.png" class="user-icon"/>
        <span style="font-size: 1rem">{{username}}</span>
      </el-header>
      <el-main>
        <component :is="currentTab" keep-alive :cObj="cObj" :cAction="cAction"></component>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import User from '../components/User'
import Article from '../components/Article'
import Editor from '../components/RichEditor'
import Consumer from '../components/Consumer'
import EventBus from '../EventBus'
// eslint-disable-next-line
import ExceptionHandler from '../ExceptionHandler'
export default {
  name: 'AdminContainer',
  data () {
    return {
      title: '',
      username: localStorage.getItem(this.GLOBAL.usernameKey),
      currentTab: 'Article',
      cObj: {},
      cAction: 'update'
    }
  },
  components: {
    User,
    Article,
    Editor,
    Consumer
  },
  methods: {
    toggleTab: function (tab, action) {
      this.currentTab = tab
      this.cAction = action
      console.log('current tab:%s  action:%s', tab, action)
    },
    handleCommand: function (command) {
      console.log(command)
      if (command === 'logout') {
        this.logout()
      }
    },
    logout: function () {
      localStorage.removeItem('token')
      localStorage.removeItem('username')
      this.$router.push({path: '/login'})
    }
  },
  mounted () {
    // switch tab
    EventBus.$on('editArticle', (row) => {
      this.cObj = row
      this.toggleTab('Editor', 'update')
    })
    EventBus.$on('toggleTab', (tab) => {
      this.toggleTab(tab, 'update')
    })
    // checkToken()
    this.checkToken(GTokenKey, GLoginTimeKey, GUsernameKey)
  }
}
</script>

<style scoped>
  .el-container{
    height: 100%;
  }
  .el-header {
    background-color: #B3C0D1;
    color: #333;
    line-height: 60px;
    text-align: right;
    font-size: 12px
  }
  .el-aside {
    height: 100%;
    background-color: #D3DCE6;
    color: #333;
    line-height: 200px;
  }

  .el-main {
    background-color: #E9EEF3;
    color: #333;
    /*line-height: 160px;*/
  }
  .user-icon {
    width: 20px;
    height: 20px;
  }
  .run_mode {
    float: left;
  }
</style>
